<?php

namespace app\admin\controller;

use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * 新订单
 *
 * admin-NewOrders
 *
 * @Apidoc\Title("新订单")
 * @Apidoc\Group("api")
 */
class NewOrders extends BaseController
{
    /**
     * 新增
     *
     * admin-NewOrders-add
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\NewOrders::create($data['new_orders']);
            return success();
        } else {
            $options = [];
            $new_orders__member_id_option = \app\common\model\Member::column('name as name, id as id');
            $options['new_orders__member_id_options'] = $new_orders__member_id_option;
            $new_orders__product_id_option = [];
            $options['new_orders__product_id_options'] = $new_orders__product_id_option;
            $new_orders__is_payment_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['new_orders__is_payment_options'] = $new_orders__is_payment_option;
            $new_orders__is_apply_for_invoice_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['new_orders__is_apply_for_invoice_options'] = $new_orders__is_apply_for_invoice_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 修改
     *
     * admin-NewOrders-update
     */
    public function update()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\NewOrders::update($data['new_orders']);
            return success();
        } else {
            $data = \app\common\model\NewOrders::find(input('new_orders__id'));
            $r = [];
            foreach ($data->getData() as $key => $val) {
                $r['new_orders__' . $key] = $val;
            }
            View::assign('r', $r);
            $options = [];
            $new_orders__member_id_option = \app\common\model\Member::column('name as name, id as id');
            $options['new_orders__member_id_options'] = $new_orders__member_id_option;
            $new_orders__product_id_option = [];
            $options['new_orders__product_id_options'] = $new_orders__product_id_option;
            $new_orders__is_payment_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['new_orders__is_payment_options'] = $new_orders__is_payment_option;
            $new_orders__is_apply_for_invoice_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['new_orders__is_apply_for_invoice_options'] = $new_orders__is_apply_for_invoice_option;
            View::assign($options);
            return View::fetch();
        }
    }

    public function update1()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\NewOrders::update($data['new_orders']);
            return success();
        } else {
            $data = \app\common\model\NewOrders::find(input('new_orders__id'));
            $r = [];
            foreach ($data->getData() as $key => $val) {
                $r['new_orders__' . $key] = $val;
            }
            View::assign('r', $r);
            $options = [];
            $new_orders__member_id_option = \app\common\model\Member::column('name as name, id as id');
            $options['new_orders__member_id_options'] = $new_orders__member_id_option;
            $new_orders__product_id_option = [];
            $options['new_orders__product_id_options'] = $new_orders__product_id_option;
            $new_orders__is_payment_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['new_orders__is_payment_options'] = $new_orders__is_payment_option;
            $new_orders__is_apply_for_invoice_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['new_orders__is_apply_for_invoice_options'] = $new_orders__is_apply_for_invoice_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 删除
     *
     * admin-NewOrders-delete
     */
    public function delete()
    {
        \app\common\model\NewOrders::destroy(input('new_orders__id'));
        return success();
    }
    /**
     * 列表
     *
     * admin-NewOrders-list
     */
    public function list()
    {
        if (request()->isPost()) {
            return error('请求方式错误');
        }
        $query = Db::table('db_new_orders');
        $query->join('db_member', 'db_member.id = db_new_orders.member_id');
        if (input('new_orders__create_time_start', '') || input('new_orders__create_time_end', '')) {
            $new_orders__create_time_start = input('new_orders__create_time_start', '', 'search_in');
            $new_orders__create_time_end = input('new_orders__create_time_end', '', 'search_in');
            $new_orders__create_time_start = $new_orders__create_time_start ? strtotime($new_orders__create_time_start) : 0;
            $new_orders__create_time_end = $new_orders__create_time_end ? strtotime($new_orders__create_time_end) + 24 * 60 * 60 - 1 : time();
            $query->where('db_new_orders.create_time', 'between', [$new_orders__create_time_start, $new_orders__create_time_end]);
        }
        if (input('new_orders__is_payment', '') !== '') {
            $query->where('db_new_orders.is_payment', '=', input('new_orders__is_payment', '', 'search_in'));
        }
        if (input('member__id', '') !== '') {
            $query->where('db_member.id', '=', input('member__id', '', 'search_in'));
        }
        $query->where('db_new_orders.delete_time', '=', null);
        $query->where('db_member.delete_time', '=', null);
        $query->field(['db_new_orders.id' => 'new_orders__id']);
        $query->field(['db_new_orders.create_time' => 'new_orders__create_time']);
        $query->withAttr('new_orders__create_time', function ($value, $data) {
            return date('Y-m-d H:i:s', $value);
        });
        $query->field(['db_new_orders.name' => 'new_orders__name']);
        $query->field(['db_new_orders.payment_method' => 'new_orders__payment_method']);
        $query->field(['db_new_orders.purchase_type' => 'new_orders__purchase_type']);
        $query->field(['db_new_orders.total_amount' => 'new_orders__total_amount']);
        $query->field(['db_new_orders.is_payment' => 'new_orders__is_payment']);
        $query->field(['db_new_orders.order_number' => 'new_orders__order_number']);
        $query->field(['db_member.name' => 'member__name']);
        $query->order('db_new_orders.id', 'desc');
        $data = $query->paginate(input('listRow') ?: 15);
        $data = ['data' => $data->items(), 'paginate' => $data->render()];
        View::assign($data);
        $options = [];
        $new_orders__is_payment_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
        $options['new_orders__is_payment_options'] = $new_orders__is_payment_option;
        $member__id_option = \app\common\model\Member::column('name as name, id as id');
        $options['member__id_options'] = $member__id_option;
        $new_orders__is_payment_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
        $options['new_orders__is_payment_options'] = $new_orders__is_payment_option;
        View::assign($options);
        return View::fetch();
    }
}